package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Files;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class FilesDao {
	DBHelp<Files> db = new DBHelp<Files>();

	public List<Files> findAll(String fileTypeId) {
		String sql = "select id,`name`,filename,filesize,createtime,employeeid,filetypeid,filesizelong from t_file where filetypeid = ?";
		
		return db.executeQueryForList(sql, new FileRowMapper(), fileTypeId);
	}
	
	private class FileRowMapper implements RowMapper<Files> {

		public Files mapperRow(ResultSet rs) throws SQLException {
			Files file = new Files();
			file.setId(rs.getString("id"));
			file.setName(rs.getString("name"));
			file.setCreatetime(rs.getString("createtime"));
			file.setFilename(rs.getString("filename"));
			file.setFilesize(rs.getString("filesize"));
			file.setEmployeeid(rs.getString("employeeid"));
			file.setFiletypeid(rs.getString("filetypeid"));
			file.setFilesizelong(rs.getString("filesizelong"));
			return file;
		}
		
	}

	public void save(Files files) {
		String sql = "insert into t_file(id,name,filename,filesize,createtime,employeeid,filetypeid,filesizelong) values (?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, files.getId(),files.getName(),files.getFilename(),files.getFilesize(),files.getCreatetime(),files.getEmployeeid(),files.getFiletypeid(),files.getFilesizelong());
	}

	public void deleteFilesById(String filesId) {
		String sql = "delete from t_file where id=?";
		db.executeSQL(sql, filesId);
	}

	public Files findFilesById(String filesId) {
		String sql = "select id,`name`,filename,filesize,createtime,employeeid,filetypeid,filesizelong from t_file where id=?";
		return db.executeQueryForObject(sql, new FileRowMapper(), filesId);
	}

	public int countFilesById(String id) {
		String sql = "SELECT COUNT(id) FROM t_file WHERE filetypeid=?";
		return db.executeQueryForCount(sql, id);
	}

	public boolean findIsHave(String fileName) {
		String sql = "select id,`name`,filename,filesize,createtime,employeeid,filetypeid,filesizelong from t_file where filename=? ";
		Files files = db.executeQueryForObject(sql, new FileRowMapper(), fileName);
		if(files == null) {
			return true;
		} else {
			return false;
		}
	}
	

	
}
